package com.quyeying.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quyeying.entity.TblMerchantEntity;
import com.quyeying.entity.TblPopularizeVideoEntity;
import com.quyeying.entity.TblProductEntity;
import com.quyeying.mapper.TblMerchantDao;
import com.quyeying.service.TblMerchantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quyeying.service.TblPopularizeVideoService;
import com.quyeying.service.TblProductService;
import com.quyeying.utils.result.ResultEnum;
import org.springframework.stereotype.Service;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ReturnPageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Map;

/**
* <p>
 * 商户表 服务实现类
 * </p>
*
* @author skx
* @since 2022-12-12
*/
@Service
public class TblMerchantServiceImpl extends ServiceImpl<TblMerchantDao, TblMerchantEntity> implements TblMerchantService {

    @Resource
    TblProductService productService;
    @Resource
    TblPopularizeVideoService popularizeVideoService;

    @Override
    public R findListByPage(Integer page, Integer pageCount){
        IPage<TblMerchantEntity> wherePage = new Page<>(page, pageCount);
        TblMerchantEntity where = new TblMerchantEntity();
        IPage<TblMerchantEntity> iPage = baseMapper.selectPage(wherePage, Wrappers.query(where));
        return R.success(ReturnPageData.fillingData(iPage));
    }

    @Override
    public R add(TblMerchantEntity tblMerchantEntity){
        QueryWrapper<TblMerchantEntity> merchantEntityQueryWrapper = new QueryWrapper<>();
        merchantEntityQueryWrapper.eq("account", tblMerchantEntity.getAccount());
        int count = baseMapper.selectCount(merchantEntityQueryWrapper);
        if(count>0) {
            return R.error(ResultEnum.USER_IS_EXISTS);
        }
        baseMapper.insert(tblMerchantEntity);
        return R.success();
    }

    @Override
    public R delete(Long id){
        baseMapper.deleteById(id);
        return R.success();
    }

    @Override
    public R updateData(TblMerchantEntity tblMerchantEntity){
        baseMapper.updateById(tblMerchantEntity);
        return R.success();
    }

    @Override
    public R findById(Long id){
        TblMerchantEntity tblMerchantEntity = baseMapper.selectById(id);
        return R.success(tblMerchantEntity);
    }

    /**
     * 根据商户账号获取商户信息
     *
     * @param account
     */
    @Override
    public TblMerchantEntity getMerchantInfo(String account) {
        QueryWrapper<TblMerchantEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", account);
        return getOne(queryWrapper);
    }

    /**
     * 获取商家人气值
     *
     * @param merchantId
     */
    @Override
    public Long getPopularity(Long merchantId) {
        QueryWrapper<TblPopularizeVideoEntity> popularizeVideoEntityQueryWrapper = new QueryWrapper<>();
        popularizeVideoEntityQueryWrapper.select("COALESCE(sum(browse_num),0) as browseNum").eq("merchant_id", merchantId);
        TblPopularizeVideoEntity browseNum = popularizeVideoService.getOne(popularizeVideoEntityQueryWrapper);
        //商品总浏览量人气
        QueryWrapper<TblProductEntity> productEntityQueryWrapper = new QueryWrapper<>();
        productEntityQueryWrapper.select("COALESCE(sum(product_browse),0) as productBrowse").eq("merchant_id", merchantId);
        TblProductEntity productBrowse = productService.getOne(productEntityQueryWrapper);
        return productBrowse.getProductBrowse()+browseNum.getBrowseNum();
    }

    /**
     * 获取首页列表对应商户信息方法
     *
     * @param merchantId
     */
    @Override
    public Map<String, Object> getMerchantMap(Long merchantId) {
        QueryWrapper<TblMerchantEntity> merchantEntityQueryWrapper = new QueryWrapper<>();
        merchantEntityQueryWrapper.select("id, merchant_name as merchantName, merchant_headimg as merchantHeadimg").eq("id", merchantId);
        Map<String, Object> tblMerchantServiceMap = getMap(merchantEntityQueryWrapper);
        tblMerchantServiceMap.put("popularity", getPopularity(merchantId));
        return tblMerchantServiceMap;
    }

    @Override
    public R findListPageSearch(Integer current, Integer size, String search) {
        return R.success(baseMapper.findListPageSearch((new Page<>(current, size)), search));
    }

    @Override
    public TblMerchantEntity getByMerchantId(Long merchantId) {
        return baseMapper.getByMerchantId(merchantId);
    }
}
